using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;

namespace Slobby.Client.Games {
    public class SupremeCommander {

        const string supcomregKey = @"SOFTWARE\THQ\Gas Powered Games\Supreme Commander - Forged Alliance";
        const string supcomregValue = "InstallationDirectory";

        const string binPostfix = @"\Supreme Commander - Forged Alliance\bin\ForgedAlliance.exe";

        static string installPath;
        static string executablePath;

        static SupremeCommander() {
            // fetches SupCom install data from the registry
            installPath = Registry.LocalMachine.OpenSubKey(supcomregKey).GetValue(supcomregValue).ToString();
            executablePath = installPath + binPostfix;
        }

        static void runSupcom(string arguments) {
            Process.Start(executablePath, arguments + " /exitongameover");
        }

        public static void Host(string playerName) {
            string args = "/hostgame udp 6112 " + playerName + " supcomgame SCMP_019";
            runSupcom(args);
        }

        public static void Join(string playerName, System.Net.IPAddress address) {
            if (address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork) {
                throw new NotSupportedException("Non IP-4 addresses are not supported (" + address.ToString() + ")");
            }

            string args = "/joingame udp " + address.ToString() + ":6112 " + playerName;
            runSupcom(args);
        }
    }
}